<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i>后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>{{$router.currentRoute.name}}</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <div style="margin-bottom: 20px;">
      <el-button type="primary" size="medium"
                 @click="$router.push('/sys-admin/permission/admin/add-new')">添加管理员</el-button>
    </div>

    <el-table :data="tableData" stripe border style="width: 100%">
      <el-table-column prop="id" label="ID" width="80" align="center"></el-table-column>
      <el-table-column label="头像" width="80" align="center">
        <template slot-scope="scope">
          <el-avatar :size="30" :src="scope.row.avatar"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="100" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="nickname" label="昵称" width="100" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="phone" label="手机号码" width="150" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="email" label="电子邮箱" width="200" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="description" label="简介" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column label="是否启用" width="100" align="center">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.enable"
              active-color="#13ce66"
              inactive-color="#ccc"
              :active-value="1"
              :inactive-value="0">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="修改密码" width="100" align="center">
        <template slot-scope="scope">
          <el-button size="small" @click="openEditPwdDialog(scope.row)">修改密码</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button size="small" icon="el-icon-edit" circle type="primary"
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button size="small" icon="el-icon-delete" circle type="danger"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin: 10px auto;text-align: right;">
      <el-pagination
          @current-change="changePageNum"
          layout="total, prev, pager, next"
          :total="total"
          :page-size="pageSize"
          :current-page="currentPage">
      </el-pagination>
    </div>

    <el-dialog title="修改密码" :visible.sync="dialogPwdFormVisible"
               @closed="resetForm('editPwdForm')">
      <el-form :model="editPwdForm" :rules="rules" ref="editPwdForm" label-width="130px">
        <el-form-item label="用户名">
          <el-input v-model="showUsername" disabled></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="editPwdForm.password" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogPwdFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditPwdForm">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑管理员" :visible.sync="dialogFormVisible"
               @closed="resetForm('editForm')">
      <el-form :model="editForm" :rules="rules" ref="editForm" label-width="130px">
        <el-form-item label="用户名">
          <el-input v-model="showUsername" disabled></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="editForm.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <el-input v-model="editForm.avatar" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="editForm.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="editForm.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="description">
          <el-input v-model="editForm.description" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: [],
      total: 0,
      pageSize: 0,
      currentPage: 1,
      dialogFormVisible: false,
      dialogPwdFormVisible: false,
      showUsername: '',
      editPwdForm: {
        id: '',
        password: ''
      },
      editForm: {
        id: '',
        nickname: '',
        avatar: '',
        phone: '',
        email: '',
        description: ''
      },
      rules: {
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        nickname: [
          { required: true, message: '请输入昵称', trigger: 'blur' },
          { min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { pattern: /^1[0-9]{10}$/, message: '手机号码格式错误', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$/, message: '邮箱格式错误', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入简介', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    openEditPwdDialog(admin){
      this.editPwdForm.id = admin.id;
      this.showUsername = admin.username;
      this.dialogPwdFormVisible = true;
    },
    submitEditPwdForm(){
      let url = "http://localhost:9481/admins/updatePwd";
      let formData = this.qs.stringify(this.editPwdForm);

      let localJwt = localStorage.getItem('localJwt');
      this.axios.create({
        'headers': {
          'Authorization': localJwt
        }
      }).post(url, formData).then((response) => {
        let jsonResult = response.data;
        if(jsonResult.state == 20000){
          this.$notify({
            title: '提示',
            message: '修改密码成功！'
          });
          this.dialogPwdFormVisible = false;
        }else if(jsonResult.state == 40400){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogPwdFormVisible = false;
              this.loadAdminList();
            }
          });
        }else{
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    openEditDialog(admin){
      let url = "http://localhost:9481/admins/standard?id="+ admin.id;

      let localJwt = localStorage.getItem('localJwt');
      this.axios.create({
        'headers': {
          'Authorization': localJwt
        }
      }).get(url).then((response) => {
        let jsonResult = response.data;
        if(jsonResult.state == 20000){
          this.showUsername = admin.username;
          this.editForm = jsonResult.data;
          this.dialogFormVisible = true;
        }else{
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAdminList();
            }
          });
        }
      });
    },
    submitEditForm(){
      let url = "http://localhost:9481/admins/update";
      let formData = this.qs.stringify(this.editForm);

      let localJwt = localStorage.getItem('localJwt');
      this.axios.create({
        'headers': {
          'Authorization': localJwt
        }
      }).post(url, formData).then((response) => {
        let jsonResult = response.data;
        if(jsonResult.state == 20000){
          this.$notify({
            title: '提示',
            message: '编辑管理员成功！'
          });
          this.dialogFormVisible = false;
          this.loadAdminList();
        }else if(jsonResult.state == 40400){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadAdminList();
            }
          });
        }else{
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    openDeleteConfirm(admin) {
      this.$confirm('此操作将永久删除【 ' + admin.id + '-' + admin.username + ' 】, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(admin);
      }).catch(() => {
      });
    },
    handleDelete(admin){
      let url = "http://localhost:9481/admins/delete";
      let formData = 'id='+admin.id;

      let localJwt = localStorage.getItem('localJwt');
      this.axios.create({
        'headers': {
          'Authorization': localJwt
        }
      }).post(url, formData).then((response) => {
        let jsonResult = response.data;
        if(jsonResult.state == 20000){
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.loadAdminList();
        }else if(jsonResult.state == 40400){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAdminList();
            }
          });
        }else if(jsonResult.state == 40900){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });

    },
    changePageNum(value){
      this.$router.replace("?pageNum="+value);
      this.loadAdminList();
    },
    loadAdminList(){
      let pageNum = this.$router.currentRoute.query.pageNum;
      if(!pageNum){
        pageNum = 1;
      }

      let url = "http://localhost:9481/admins/list?pageNum="+ pageNum;
      console.log("url",url)
      let localJwt = localStorage.getItem('localJwt');
      this.axios.create({
        'headers': {
          'Authorization': localJwt
        }
      }).get(url).then((response) => {
        let jsonResult = response.data;
        if(jsonResult.state == 20000){
          this.tableData = jsonResult.data.list;
          this.total = jsonResult.data.total;
          this.pageSize = jsonResult.data.pageSize;
          this.currentPage = jsonResult.data.currentPage;
        }else{
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadAdminList();
  }
}
</script>